package com.rp.news.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 通用swagger
 */
@Configuration
@EnableSwagger2 // 标记项目启用 Swagger API 接口文档
@EnableAutoConfiguration
@EnableKnife4j
public class SwaggerConfigure {


	@Bean
	public Docket createRestApi(){
		Predicate<RequestHandler> userPredicate = RequestHandlerSelectors.basePackage("com.rp.user.controller");
		Predicate<RequestHandler> adminPredicate = RequestHandlerSelectors.basePackage("com.rp.admin.controller");
		Predicate<RequestHandler> articleHtmlPredicate = RequestHandlerSelectors.basePackage("com.rp.article.html.controller");
		Predicate<RequestHandler> articlePredicate = RequestHandlerSelectors.basePackage("com.rp.article.controller");
		Predicate<RequestHandler> filesPredicate = RequestHandlerSelectors.basePackage("com.rp.files.controller");

		return new Docket(DocumentationType.SWAGGER_2)
			.apiInfo(apiInfo())
			.select()
			.apis(
				Predicates.or(
						userPredicate,articleHtmlPredicate,articlePredicate,filesPredicate,
						adminPredicate)
			)
			.paths(PathSelectors.any())
			.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
			.title("新闻 - API文档")
			.description("新闻 - API文档")
			.contact("rp")
			.version("1.0")
			.build();
	}

}
